//animation example 1
//uses the animation module to read some animations and graphics and show them in a timer loop
#include "..\..\..\axl_animations.h"
#include <time.h>
using namespace AXL_Projects;

//stuff for the timers
volatile int xframe_count;
volatile int fps;
volatile int xgame_time;
int current=0;
int thepage=0;

//main game variables
static BITMAP* DblBuffer[2];
static AnimationLibrary* GameLibrary;		//library of available animations
static Animation* SpawnedAnimations[40000];	//for showing lots of them

//this example is using a standard timer based FPS loop to keep program running smoothly and to time
//called every second to store the fps for display only
void xFpsHandler()
{
 //called every 1000 milliseconds
 fps=xframe_count; //set fps to display to user
 xframe_count=0;   //reset the count for the next loop
}
END_OF_FUNCTION(fps_handler);

//timer for the game loop for keeping fps called every required FPS value
void xgame_timer()
{
	xgame_time++;
}
END_OF_FUNCTION(xgame_timer);

//set up allegro
bool Init()
{
	DblBuffer[0]=DblBuffer[1]=NULL;
	allegro_init();
	install_timer();
	install_keyboard();

	//fps timer and other timer locks
	fps=xframe_count=0;
	LOCK_VARIABLE(fps);
	LOCK_VARIABLE(xframe_count);
	LOCK_FUNCTION(xFpsHandler);
	LOCK_FUNCTION(xgame_timer);
	install_int(xFpsHandler,1000);
	install_int_ex(xgame_timer,BPS_TO_TIMER(60));

	set_color_depth(32);
//choose your option
	//if(set_gfx_mode(GFX_DIRECTX_WIN,640,480,0,0)<0) return false;
	//if(set_gfx_mode(GFX_AUTODETECT,640,480,0,0)<0) return false;
	if(set_gfx_mode(GFX_AUTODETECT_FULLSCREEN,640,480,0,0)<0) return false;
	//if(set_gfx_mode(GFX_SAFE,640,480,0,0)<0) return false;
	//if(set_gfx_mode(GFX_GDI,640,480,0,0)<0) return false;
//	text_mode(-1);
	DblBuffer[0]=create_system_bitmap(640,480);
	//DblBuffer[0]=create_video_bitmap(640,480);
	//DblBuffer[1]=create_video_bitmap(640,480);
	//if (DblBuffer1==NULL) DblBuffer1=create_bitmap(640,480);
	if (DblBuffer[1]==NULL) return false;
	return true;
}


//loop for animation
int main(void)
{
	bool done=false;
	bool draw=false;
	bool CanMove=true;
	bool FirstSprite=true;
	bool ShowSpawned=false;
	int CollisionTest=0;

	int w,h,logow,logoh;
	int coll_x=503;
	int coll_y=218;
	int speed;
	int keypress;

	srand ( time(NULL) );
	xgame_time=1;
	AnimationLibrary::LogEntry("start",true,"test.log");	//log somestuff
	Init();

	SpawnedAnimations[0]=NULL;	//quickly show not set up yet

	AnimationLibrary::LogEntry("initialised",false,"test.log");

	//create an animation repository and load the animations
	GameLibrary=new AnimationLibrary();
	if(GameLibrary->LoadError)
	{
		AnimationLibrary::LogEntry("Failed to load XML. Check it is valid",false,"test.log");
		delete GameLibrary;
		return 1;
	}
	
	current=25000;
for(int k=0;k<current;k++)
{
		SpawnedAnimations[k]=new Animation(GameLibrary,"fuel");
		SpawnedAnimations[k]->SetNewSpeedMS(rand()%150+50);
}

	//main timer loop
	//each keypress does what is says
	do {
		while(xgame_time!=0) 
		{
			draw=false;
			CanMove=false;	//manual mode only
			if(keypressed())
			{
				keypress=readkey()>>8;
				switch(keypress)
				{
				case KEY_ESC:
					done=true;
					break;
				}
				clear_keybuf();
			}

			//main display sprite
			if(done==false) 
			{
    			if(current>0)
    			{
    					for(int k=0;k<current;++k)
    					{
    						SpawnedAnimations[k]->NextMove();
    					}
    			}
				draw=true;
			}

			xgame_time--;
		}
		//should exit loop every FRAMERATE/second second if on time
		//update display if frame has been drawn
		//if not then system is too fast so don't do anything
		if(draw) 
		{
			//function call order is z order
			clear_to_color(DblBuffer[thepage],makecol(0,0,0));
			textprintf_ex(DblBuffer[thepage],font,0,0,makecol(255,255,255),-1,"sprites: %d fps %d",current,fps);
			if(current>0)
			{
					for(int k=0;k<current;++k)
					{
						draw_sprite(DblBuffer[thepage],SpawnedAnimations[k]->ReadOnly_CurrentItem,rand()%6000,rand()%400);
					}
			}

				blit(DblBuffer[0], screen, 0, 0, 0, 0,640,480);
				//show_video_bitmap(DblBuffer[thepage]);
				//thepage=(thepage+1)%2;
			xframe_count++; //update every drawn screen - should match fps which is required FPS or less, cannot be higher
			draw=false;
		}

	} while(done==false);

	//we created these, so we delete them
	for(int l=0;l<current;++l)
		delete SpawnedAnimations[l];

	//this will destroy the library, unload all loaded graphics and free up all memory
	delete GameLibrary;

	if(DblBuffer[0]!=NULL) {destroy_bitmap(DblBuffer[0]);}
	//if(DblBuffer[1]!=NULL) {destroy_bitmap(DblBuffer[1]);}
	remove_int(xFpsHandler);
	remove_int(xgame_timer);

	//allegro_exit();

	return 0;
}
END_OF_MAIN()

//just in case allegro_core is included for logging, need this function
void GameLoop() {}

